package ldh.im.fxbase.service;

import ldh.im.fxbase.dao.ImFileDao;
import ldh.im.fxbase.dao.ImSessionDao;
import ldh.im.fxbase.data.dto.ImFileSearch;
import ldh.im.fxbase.data.dto.PageResult;
import ldh.im.fxbase.data.pojo.ImFile;
import ldh.im.fxbase.data.pojo.ImMessage;
import ldh.im.fxbase.data.pojo.ImSession;
import ldh.im.fxbase.util.BeanFactory;
import ldh.im.fxbase.util.FileUtil;
import ldh.im.util.UploadFileUtil;

import java.io.File;
import java.sql.SQLException;
import java.util.List;

public class FileService {

    private ImSessionDao imSessionDao = BeanFactory.getInstance().getBean(ImSessionDao.class);
    private ImFileDao imFileDao = BeanFactory.getInstance().getBean(ImFileDao.class);

    public void saveFile(String userId, ImMessage imMessage) throws SQLException {
        if (!imMessage.getMessageType().isFile()) return;

        String suffix = UploadFileUtil.suffix(imMessage.getContent());
        imMessage.initMessageExt();
        String sign = imMessage.getMessageExt().getMd5();
        ImFile dbFile = imFileDao.getBySign(userId, sign);
        if (dbFile != null) {
            return;
        }
        String newFileName = imMessage.getMessageExt().getOriginalName();
        String file = imMessage.getContent();

        ImFile imFile = new ImFile();
        imFile.setFileName(newFileName);
        imFile.setRelativePath(imMessage.getMessageExt().getRemote());
        imFile.setPath(file);
        Long fileSize = FileUtil.getFileSize(new File(FileUtil.getFilePath(imMessage)));
        imFile.setFileSize(fileSize.intValue());
        imFile.setSuffix(suffix);
        ImSession imSession = imMessage.getImSession();
        if (imSession == null) {
            imSession = imSessionDao.getSessionById(imMessage.getSessionId());
        }
        imFile.setSessionId(imSession.getServerId());
        imFile.setUserId(imMessage.getFromUserId());
        imFile.setCurrentUserId(userId);
        imFile.setMessageId(imMessage.getServerId());

        insert(imFile);
    }

    public void deleteFile(ImFile imFile) throws SQLException {
        imFileDao.delete(imFile);
    }

    public void deleteFile(String userId, String sessionId, String messageId) throws SQLException {
        imFileDao.delete(userId, sessionId, messageId);
    }

    public ImFile getFile(String userId, String sign) throws SQLException {
        return imFileDao.getBySign(userId, sign);
    }

    public List<ImFile> search(String userId, ImFileSearch search) throws SQLException {
        List<ImFile> imFiles = imFileDao.search(userId, search);
        for (ImFile imFile : imFiles) {
            ImSession imSession = imSessionDao.getSessionByServerId(userId, imFile.getSessionId());
            imFile.setSession(imSession);
        }
        return imFiles;
    }

    public PageResult searchForPage(String userId, ImFileSearch search) throws SQLException {
        List<ImFile> imFiles = imFileDao.search(userId, search);
        int total = imFileDao.searchTotal(userId, search);
        for (ImFile imFile : imFiles) {
            ImSession imSession = imSessionDao.getSessionByServerId(userId, imFile.getSessionId());
            imFile.setSession(imSession);
        }
        return new PageResult(imFiles, total);
    }

    private void insert(ImFile imFile) throws SQLException {
        try {
            imFileDao.insert(imFile);
        } catch (SQLException sql) {
            if (sql.getErrorCode() == 1062) { // 在并发下，会出现主键冲突的可能
                // do nothing
            } else {
                throw sql;
            }
        }

    }
}
